package com.yitongmao.core.glide;


import com.alibaba.sdk.android.oss_android_sdk.BuildConfig;
import com.yitongmao.core.network.httpclient.DownloadProgressInterceptor;
import com.yitongmao.core.network.httpclient.OkHttpLogInterceptor;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;

/**
 * Glide加载图片进度显示
 */
public class GlideProgressListener {

    private GlideProgressListener() {
    }

    private static OkHttpClient glideOkHttpClient;
    private static OkHttpLogInterceptor logging;

    /**
     * List of listeners
     */
    private static final List<WeakReference<ProgressListener>> glideProgressListeners = Collections.synchronizedList(new ArrayList<WeakReference<ProgressListener>>());

    /**
     * Interceptor listener
     */
    private static final ProgressListener glideDownloadProgressListener = new ProgressListener() {
        @Override
        public void update(long bytesRead, long contentLength, boolean done) {
            for (int i = 0, j = glideProgressListeners.size(); i < j; i++) {
                WeakReference<ProgressListener> weakListener = glideProgressListeners.get(i);
                ProgressListener listener = weakListener.get();
                if (listener == null) {
                    glideProgressListeners.remove(i);
                    i--;
                } else {
                    listener.update(bytesRead, contentLength, done);
                }
            }
        }
    };

    /**
     * 给okhttp加拦截器
     *
     * @return
     */
    public static OkHttpClient getGlideOkHttpClient(ProgressListener progressListener) {
        if (glideOkHttpClient == null) {
            if (BuildConfig.DEBUG && logging == null) {
                logging = new OkHttpLogInterceptor();
                logging.setLevel(OkHttpLogInterceptor.Level.BODY);
            }
            glideOkHttpClient = new OkHttpClient().newBuilder().addNetworkInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Response originalResponse = chain.proceed(chain.request());
                    return originalResponse.newBuilder()
                            .body(new ProgressResponseBody(originalResponse.body(), glideDownloadProgressListener))
                            .build();
                }
            }).build();
            glideOkHttpClient.newBuilder().addInterceptor(new DownloadProgressInterceptor(progressListener)).build();

        }
        if (BuildConfig.DEBUG && logging != null) {
           // return glideOkHttpClient.newBuilder().addInterceptor(logging).addInterceptor((new GzipResponseInterceptor())).build();
            return glideOkHttpClient.newBuilder().addInterceptor(logging).build();
        }
        return glideOkHttpClient;
    }

    /**
     * 添加进度监听
     *
     * @param listener
     */
    public static void addGlideProgressListener(ProgressListener listener) {
        if (listener != null) {
            if (findGlideProgressListener(listener) != null) {
                return;
            } else {
                glideProgressListeners.add(new WeakReference<ProgressListener>(listener));
                //Log.d(GlideProgressListener.class.getSimpleName(), "Glide download listener add");
            }
        }
    }

    /**
     * 删除监听
     *
     * @param listener
     */
    public static void removeGlideProgressListener(ProgressListener listener) {
        if (listener != null) {
            WeakReference<ProgressListener> founded = findGlideProgressListener(listener);
            if (founded != null) {
                glideProgressListeners.remove(founded);
                //Log.d(GlideProgressListener.class.getSimpleName(), "Glide download listener remove");
            }
        }
    }

    private static WeakReference<ProgressListener> findGlideProgressListener(ProgressListener listener) {
        List<WeakReference<ProgressListener>> listeners = glideProgressListeners;
        for (int i = 0, j = listeners.size(); i < j; i++) {
            WeakReference<ProgressListener> wpl = listeners.get(i);
            if (wpl.get() == listener) {
                return wpl;
            }
        }
        return null;
    }
}